import { loginAPI, getProfileAPI } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    profile: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 清除用户信息
    removeToken(state) {
      state.token = ''
      state.profile = {}
      removeToken()
    },
    // 设置用户信息
    setProfile(state, profile) {
      state.profile = profile
    }
  },
  actions: {
    async loginApi(context, data) {
      const res = await loginAPI(data)
      context.commit('setToken', res.data.token)
    },

    // 获取用户信息
    async getProfile(context) {
      const res = await getProfileAPI()
      context.commit('setProfile', res.data)
      console.log('res.data: ', res.data)
      return res.data.permissions
    }
  }
}
